<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .box {
        width: 100px;
        height: 100px;
        background-color: skyblue;
        position: absolute;
        top: 200px;
        left: 300px;
      }
    </style>
  </head>
  <body>
    <box class="box" id="box"></box>
    <script>
      var box = document.getElementById('box')
      var result = box.getBoundingClientRect()
      //   获取当前元素距离视口左侧距离
      console.log(result.left)
      //   获取当前元素距离视口顶部距离
      console.log(result.top)
    </script>
  </body>
</html>
